﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
namespace ATui
{
    class sendATcmd
    {
        // define serial port
        private SerialPort _sp;
        public SerialPort sp
        {
            get
            {
                return _sp;
            }

            set
            {
                _sp = value;
            }
        }

        // prase how many command in this test
        public List<string> praseTextCmd(string text)
        {
            List<string> atCmdList = new List<string>();

            text += "\r\n";
            char[] textChar = text.ToArray();

            int cmdBeginIndex = 0;
            for (int i = 0; i < textChar.Count(); ++i)
            {
                if (textChar[i] == '\r')
                {
                    if (textChar[i + 1] == '\n')
                    {
                        string cmd = "";
                        for (int p = cmdBeginIndex; p < i; ++p)
                        {
                            if (textChar[p] != '\r' && textChar[p] != '\n')
                            {
                                cmd += textChar[p];
                            }
                        }
                        atCmdList.Add(cmd);

                        cmdBeginIndex = i + 2;
                    }
                }
            }

            return atCmdList;
        }

        // send at command to serial port
        public string sendSerialAtCmd(string input, bool isHex)
        {
            if (sp == null)
            {
                return "请先选择串口!" + "\r\n";
            }

            try
            {
                if (sp.IsOpen == false)
                {
                    //open serial port 
                    sp.Open();
                }

                // declare output string
                string AT_output = "";

                if (isHex == true)
                {
                    // HEX input
                    byte[] data_input = Encoding.Unicode.GetBytes(input);

                    string hex_input = Convert.ToBase64String(data_input);


                    //write line to serial port 
                    sp.WriteLine(hex_input);
                    //set sleep time 500 ms 
                    Thread.Sleep(500);

                    //set read time out to 500 ms 
                    sp.ReadTimeout = 500;

                    // output sp feedback
                    while (sp.BytesToRead != 0)
                    {
                        byte[] data_output = Convert.FromBase64String(sp.ReadLine());
                        AT_output += Encoding.Unicode.GetString(data_output) + "\r\n";
                    }

                }
                else
                {
                    // ASC2 input

                    //write line to serial port 
                    sp.WriteLine(input);
                    //set sleep time 500 ms 
                    Thread.Sleep(500);
        
                    //set read time out to 500 ms 
                    sp.ReadTimeout = 500;

                    // output sp feedback
                    while (sp.BytesToRead != 0)
                    {
                        AT_output += sp.ReadLine() + "\r\n";
                    }
                    
                }

                //close serial port 
                sp.Close();

                return AT_output;
            }
            catch (System.Exception ex)
            {
                return ex.ToString() + "\r\n";
            }  
        }
    }
}
